{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nGiven an int array which might contain duplicates, find the largest subset of it which form a sequence.\\nEg. {1,6,10,4,7,9,5}\\nthen ans is 4,5,6,7\\nSorting is an obvious solution. Can this be done in O(n) time\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "Given an int array which might contain duplicates, find the largest subset of it which form a sequence.\n",
    "Eg. {1,6,10,4,7,9,5}\n",
    "then ans is 4,5,6,7\n",
    "Sorting is an obvious solution. Can this be done in O(n) time\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find(arr):\n",
    "    table = {}\n",
    "    first = 0\n",
    "    last = 0\n",
    "    for i in arr:\n",
    "        beg = end = i\n",
    "        if i in table:\n",
    "            print (f\"skip {i}\")\n",
    "            continue\n",
    "        table[i] = 'EXISTED'\n",
    "        print(f'insert {i} into Table')\n",
    "        if i - 1 in table:\n",
    "            beg = table[i-1]\n",
    "        if i + 1 in table:\n",
    "            end = table[i+1]\n",
    "        table[beg] = end\n",
    "        table[end] = beg\n",
    "        print(f\"update begin: T[{beg}] = {end}\\nend: T[{end}] = {beg}\")\n",
    "        if end - beg > last - first:\n",
    "            first = beg\n",
    "            last = end\n",
    "    return list(range(first, last + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "insert 1 into Table\n",
      "update begin: T[1] = 1\n",
      "end: T[1] = 1\n",
      "insert 6 into Table\n",
      "update begin: T[6] = 6\n",
      "end: T[6] = 6\n",
      "insert 10 into Table\n",
      "update begin: T[10] = 10\n",
      "end: T[10] = 10\n",
      "insert 4 into Table\n",
      "update begin: T[4] = 4\n",
      "end: T[4] = 4\n",
      "insert 7 into Table\n",
      "update begin: T[6] = 7\n",
      "end: T[7] = 6\n",
      "insert 9 into Table\n",
      "update begin: T[9] = 10\n",
      "end: T[10] = 9\n",
      "insert 5 into Table\n",
      "update begin: T[4] = 7\n",
      "end: T[7] = 4\n",
      "skip 5\n",
      "insert 8 into Table\n",
      "update begin: T[4] = 10\n",
      "end: T[10] = 4\n",
      "[4, 5, 6, 7, 8, 9, 10]\n"
     ]
    }
   ],
   "source": [
    "arr = [1,6,10,4,7,9,5, 5,8]    \n",
    "print(find(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "insert 1 into Table\n",
      "update begin: T[1] = 1\n",
      "end: T[1] = 1\n",
      "insert 5 into Table\n",
      "update begin: T[5] = 5\n",
      "end: T[5] = 5\n",
      "skip 5\n",
      "insert 6 into Table\n",
      "update begin: T[5] = 6\n",
      "end: T[6] = 5\n",
      "insert 10 into Table\n",
      "update begin: T[10] = 10\n",
      "end: T[10] = 10\n",
      "insert 4 into Table\n",
      "update begin: T[4] = 6\n",
      "end: T[6] = 4\n",
      "insert 7 into Table\n",
      "update begin: T[4] = 7\n",
      "end: T[7] = 4\n",
      "insert 9 into Table\n",
      "update begin: T[9] = 10\n",
      "end: T[10] = 9\n",
      "insert 8 into Table\n",
      "update begin: T[4] = 10\n",
      "end: T[10] = 4\n",
      "[4, 5, 6, 7, 8, 9, 10]\n"
     ]
    }
   ],
   "source": [
    "arr = [1, 5, 5, 6,10,4,7,9,8]    \n",
    "print(find(arr))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
